<?php
/**
 * Created by PhpStorm.
 * User: 小蛮哼哼哼
 * Email: 243194993@qq.com
 * Date: 2021/12/31 0031
 * Time: 11:48
 * motto: 现在的努力是为了小时候吹过的牛逼！
 */

declare(strict_types=1);


namespace app\common\business;

use app\common\lib\Show;
use app\common\model\MallCart as ModelObj;
use app\common\validate\Cart as ValObj;
use think\Exception;

class Cart extends Base
{
    /**
     * @var \app\common\validate\Cart
     */
    protected ValObj $valObj;
    /**
     * @var \app\common\model\MallCart
     */
    protected ModelObj $modelObj;

    /**
     *
     */
    public function __construct()
    {
        $this->valObj = new ValObj();
        $this->modelObj = new ModelObj();
    }

    /**
     * 加入购物车
     * @param $data
     * @return void
     */
    public function saveData($data){
        try {
            $result = $this->modelObj->save($data);

        } catch (\Exception $e) {
            throw new Exception("写入失败");
        }

        return $result;
    }

    /**
     * 更新购物车商品数量
     * @param $data
     * @return bool
     * @throws Exception
     */
    public function  changeData($where,$data){

        try {
            $result = $this->modelObj->where($where)->save($data);
        } catch (\Exception $e) {
            throw new Exception("操作失败");
        }
        return $result;
    }

    /**
     * 查询指定购物车信息
     * @param $where
     * @param $field
     * @return array|\think\Model|null
     * @throws Exception
     */
    public function  getCartByInfo($where,$field){
        try {
            $result = $this->modelObj->getModelByInfo($where,$field);
        } catch (\Exception $e) {
            throw new Exception("操作失败");
        }
        return $result;
    }

    /**
     * 删除购物车
     * @param $cart_ids
     * @return mixed
     */
    public function deleteCartByIds($cart_ids){
        $cartids =explode(',',$cart_ids);
        $cids =[];
        foreach ($cartids as $k=>$v){
            $cids[] =$v;
        }
        $where =[
            ['id','in',$cids]
        ];
        $result =$this->modelObj->deleteByIds($where);

        return $result;
    }

    /**
     *  获取购物车商品
     * @param $where
     * @return array
     * @throws Exception
     */
    public function  getCartsList($openid){

        try{
           $mlist = $this->modelObj->getModelByGroup([['openid','=',$openid]],'manager_id','manager_id');
        }catch (\Exception $e){
            throw new Exception("查询失败");
        }
        if($mlist){
            foreach ($mlist as $kk=>$vv){
                $minfo =(new Manager())->getManagerInfo([['id','=',$vv['manager_id']]],'title');

                $mlist[$kk]['title'] =$minfo['title'];
                $cartlist= $this->modelObj->getModelByListToArray([['manager_id','=',$vv['manager_id']],['openid','=',$openid]],'id,goodsid,total','create_time desc');

                foreach($cartlist as $k=>$v){
                    $goods =(new Goods())->getGoodsInfo($openid,[['id','=',$v['goodsid']]],'id,title,title_desc,logo,goods_price');

                    $cartlist[$k]['title'] =$goods['title'];
                    $cartlist[$k]['title_desc'] =$goods['title_desc'];
                    $cartlist[$k]['logo'] =$goods['logo'];
                    $cartlist[$k]['goods_price'] =$goods['user_price'];
                    $this->modelObj->where("id='".$v['id']."'")->save(['goods_price'=>$goods['user_price']]);
                 }
                $mlist[$kk]['goods'] =$cartlist;
            }
       }
        return $mlist;
    }

    /**
     *  获取选择购物车商品
     * @param $where
     * @return array
     * @throws Exception
     */
    public function  getCartsToByList($cart_ids,$openid){

        $cartids =explode(',',$cart_ids);
        $cids =[];
        foreach ($cartids as $k=>$v){
            $cids[] =$v;
        }
        $where =[
            ['id','in',$cids],
            ['openid','=',$openid]
        ];

        try{
            $mlist = $this->modelObj->getModelByGroup($where,'manager_id','manager_id');
        }catch (\Exception $e){
            throw new Exception("查询失败");
        }
        $totalprice =0;
        $tdeduct_price =0;
        if($mlist){
            foreach ($mlist as $kk=>$vv){
                $minfo =(new Manager())->getManagerInfo([['id','=',$vv['manager_id']]],'title,lng,lat');
                $mlist[$kk]['title'] =$minfo['title'];
                $cartlist= $this->modelObj->getModelByListToArray([['manager_id','=',$vv['manager_id']],['openid','=',$openid],['id','in',$cids]],'id,goodsid,total','create_time');
                foreach($cartlist as $k=>$v){
                    $goods =(new Goods())->getGoodsInfo($openid,[['id','=',$v['goodsid']]],'*');
                    $cartlist[$k]['title'] =$goods['title'];
                    $cartlist[$k]['title_desc'] =$goods['title_desc'];
                    $cartlist[$k]['logo'] =$goods['logo'];
                    $cartlist[$k]['goods_price'] =$goods['user_price'];
                    $totalprice+=round($v['total']*$goods['user_price'],2);
                    $tdeduct_price+=$goods['deduct_price'];
                }
                $mlist[$kk]['goods'] =$cartlist;
                $mlist[$kk]['lng'] =$minfo['lng'];
                $mlist[$kk]['lat'] =$minfo['lat'];
            }
        }

        return array('mlist'=>$mlist,'totalprice'=>$totalprice,'tdeduct_price'=>$tdeduct_price);
    }


}